package com.fable.sp.core.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ReflectionUtils;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;

/**
 * .
 *
 * @author stormning on 16/8/30.
 */
public class BeanUtils extends org.springframework.beans.BeanUtils {
    private static final Logger LOGGER = LoggerFactory.getLogger(BeanUtils.class);

    public static void setProperty(Object obj, String propName, Object value) {
        PropertyDescriptor descriptor = org.springframework.beans.BeanUtils.getPropertyDescriptor(obj.getClass(), propName);
        Method writeMethod = descriptor.getWriteMethod();
        ReflectionUtils.makeAccessible(writeMethod);
        ReflectionUtils.invokeMethod(writeMethod, obj, value);
    }

    public static Object getProperty(Object obj, String propName) {
        PropertyDescriptor descriptor = org.springframework.beans.BeanUtils.getPropertyDescriptor(obj.getClass(), propName);
        Method readMethod = descriptor.getReadMethod();
        ReflectionUtils.makeAccessible(readMethod);
        return ReflectionUtils.invokeMethod(readMethod, obj);
    }
}
